package ps.client.plugin.eq2.log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class DeathTrigger extends Trigger {

	public static final String NAME = "Death";
	public static final String REGEX = ".*ha(s|ve) killed.*";
	public static final int FLAGS = IGNORE_CHAT;
	
	String attacker = "*";
	String victim = "*";

	public DeathTrigger() {
		super(NAME, REGEX, FLAGS);
	}
	
	public String getAttacker() {
		return attacker;
	}
	
	public String getVictim() {
		return victim;
	}
	
	public void fireTriggerEvent(TriggerEvent e) {
		String content = e.getLogLine().getContent();
		int index = content.indexOf(" has killed");
		index = index < 0 ? content.indexOf(" have killed") : index;
		if (index > 0) {
			attacker = content.substring(0, index);
		}
		index = content.indexOf(" has killed");
		index = index < 0 ? content.indexOf("have killed") : index;
		if (index >= 0 && content.length() > index + 13) {
			victim = content.substring(index + 12, content.length() - 1);
		}
		
		super.fireTriggerEvent(e);
	}
	
	public static void main(String[] args) {
		DeathTrigger trigger = new DeathTrigger();
		String line = "(1243101493)[Sat May 23 19:58:13 2009] Sornnn has killed a skeletal guardian.";
		Matcher matcher = Pattern.compile(REGEX).matcher(line);
		if (matcher.matches()) {
			trigger.fireTriggerEvent(new TriggerEvent(trigger, new LogLine(null, line), matcher));
			System.out.println("attacker: \"" + trigger.getAttacker() + "\"");
			System.out.println("victim: \"" + trigger.getVictim() + "\"");
		}
	}
}
